<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">ADMIN</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('index', 'Index', 'index')">前台</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('admin', 'Index', 'index')">看板</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('admin', 'User', 'index')">用户</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('admin', 'Role', 'index')">角色</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('admin', 'Node', 'index')">节点</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" onclick="checkBeforeJump('admin', 'Notice', 'index')">公告</a>
        </li>
        <!-- <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            其他管理
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="#">其他管理1</a></li>
            <li><a class="dropdown-item" href="#">其他管理2</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="#">其他管理3</a></li>
          </ul>
        </li> -->
      </ul>
      <ul class="navbar-nav d-flex">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            <span id="nav-username"></span>
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="#" onclick="openNavInfoModal()">基本信息</a></li>
            <li><a class="dropdown-item" href="#" onclick="openNavSetPswModal()">重设密码</a></li>
          </ul>
        </li>
        <li class="nav-item"><a class="nav-link" href="#" onclick="logout()"><i class="bi bi-power"></i>退出</a></li>
      </ul>
    </div>
  </div>
</nav>

<!-- setPswModal -->
<div class="modal fade" id="nav-setPswModal" tabindex="-1" data-bs-backdrop="static" data-bs-keyboard="false" aria-labelledby="nav-setPswModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="nav-setPswModalLabel">设置密码</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body" id="addForm">
        <form class="mb-5 ms-1 me-1">
          <div class="mb-3">
            <label for="nav-old_password" class="form-label">旧密码</label>
            <input type="password" class="form-control" id="nav-old_password" name="old_password" required>
          </div>
          <div class="mb-3">
            <label for="nav-new_password" class="form-label">新密码</label>
            <input type="password" class="form-control" id="nav-new_password" name="new_password" required>
          </div>
          <div class="mb-3">
            <label for="nav-again_password" class="form-label">再次输入新密码</label>
            <input type="password" class="form-control" id="nav-again_password" name="again_password" required>
          </div>
          <button type="button" class="btn btn-primary" id="nav-savePswBtn">保存</button>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- infoModal -->
<div class="modal fade" id="nav-infoModal" tabindex="-1" data-bs-backdrop="static" data-bs-keyboard="false" aria-labelledby="infoModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="infoModalLabel">用户基本信息</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body" id="infoForm">
        <form class="mb-5 ms-1 me-1">
          <div class="mb-3">
            <label for="nav-info-name_zh" class="form-label">中文名</label>
            <input type="text" class="form-control" id="nav-info-name_zh" name="name_zh" required>
          </div>
          <div class="mb-3">
            <label for="nav-info-username" class="form-label">用户名</label>
            <input type="text" class="form-control" id="nav-info-username" name="username">
          </div>
          <div class="mb-3">
            <label for="nav-info-phone" class="form-label">手机号</label>
            <input type="text" class="form-control" id="nav-info-phone" name="phone" required>
          </div>
          <button type="button" class="btn btn-primary" id="nav-info-editBtn">保存</button>
        </form>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  // 设置密码模态框
  function openNavSetPswModal() {
    $('#nav-old_password').val('');
    $('#nav-new_password').val('');
    $('#nav-again_password').val('');
    $('#nav-setPswModal').modal('show');
  }

  // 设置基本信息模态框
  function openNavInfoModal() {
    $.ajax({
        url: '?m=admin&c=User&a=getUserBySessionUid',
        type: 'post',
        dataType: 'json',
        data: {},             
        success: function(data){
            if (data.code == 200) {
                $('#nav-info-name_zh').val(data.data.name_zh);
                $('#nav-info-username').val(data.data.username);
                $('#nav-info-phone').val(data.data.phone);
                $('#nav-infoModal').modal('show');
            }else{
                layer.msg(data.msg);
            }  
        }
    });
  }

  // 加载用户名
  $(document).ready(function() {
    $.ajax({
        url: '?m=admin&c=User&a=getUserBySessionUid',
        type: 'post',
        dataType: 'json',
        data: {},             
        success: function(data){
            if (data.code == 200) {
                $('#nav-username').text(data.data.name_zh);
            }else{
                $('#nav-username').text('未知用户');
            }  
        }
    });

    //更新密码
    $('#nav-savePswBtn').click(function(event) {
        var new_password = $('#nav-new_password').val();
        var again_password = $('#nav-again_password').val();
        if (new_password != again_password) {
          layer.msg('两次输入新密码不一致！');
          return false;
        }
        var field = {
          old_password: $('#nav-old_password').val(),
          new_password: new_password,
          again_password: again_password,
        };
        $.ajax({
            url: '?m=admin&c=User&a=updatePasswordBySelf',
            type: 'post',
            dataType: 'json',
            data: field,              
            success: function(data){
                if (data.code == 200) {
                    var index = layer.alert(data.msg, {
                        title: '提示'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        $('#nav-setPswModal').modal('hide');
                    });
                }else{
                    layer.msg(data.msg);
                }  
            }
        });
    });

    //更新基本信息
    $('#nav-info-editBtn').click(function(event) {
        if ($('#nav-info-name_zh').val()=='') {
          layer.msg('中文名不能为空！');
        }
        if ($('#nav-info-phone').val()=='') {
          layer.msg('手机号不能为空！');
        }
        var field = {
          name_zh: $('#nav-info-name_zh').val(),
          username: $('#nav-info-username').val() ? $('#nav-info-username').val() : $('#nav-info-phone').val(),
          phone: $('#nav-info-phone').val(),
        };
        $.ajax({
            url: '?m=admin&c=User&a=updateUserInfo',
            type: 'post',
            dataType: 'json',
            data: field,              
            success: function(data){
                if (data.code == 200) {
                    var index = layer.alert(data.msg, {
                        title: '提示'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        $('#nav-infoModal').modal('hide');
                    });
                }else{
                    layer.msg(data.msg);
                }  
            }
        });
    });

  });
</script>